package ui;

import game.Game;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class GameCompletionState extends BaseMenuState {
	private UnicodeFont resultFont;
	private boolean transitioning;
	private ArrayList<ButtonComponent> menuItems = new ArrayList<ButtonComponent>();

	public GameCompletionState() {
		super();
		try {
			resultFont = new UnicodeFont("resources/menu/HARLOWSI.TTF", 50, false, false);
			resultFont.getEffects().add(new ColorEffect(java.awt.Color.white));
			resultFont.addAsciiGlyphs();
			resultFont.loadGlyphs();
		} catch (SlickException e) {
			resultFont = null;
			e.printStackTrace();
		}
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame state) throws SlickException {
		// draw menu buttons - new game, options, exit		
		addButton(gc, "Next level", GameStateSwitcher.GAME_STATE);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame state, Graphics g) throws SlickException {
		g.drawImage(new Image("resources/menu/title.png"),
				gc.getWidth() / 2 - titleWidth / 2, componentSpacing);
		boolean didSucceed = ((GameStateSwitcher) state).getSuccess();
		g.setFont(resultFont);
		boolean onLastLevel = ((GameStateSwitcher)state).getCurrentLevel() == GameWindowContainer.finalLevel;
		if (!transitioning){
			if (didSucceed){
				g.drawString(onLastLevel ? "You are fully evolved!" : "What? You are evolving!", onLastLevel ? gc.getWidth() / 2 - 200 : gc.getWidth() / 2 - 200, titleHeight);			
			}
			else if (!didSucceed)
				g.drawString("You have become extinct!", gc.getWidth() / 2 - 200, titleHeight);
		}
		g.setFont(gc.getDefaultFont());
		for (ButtonComponent button : menuItems){
			if (button.getText().equals("Next level") && !didSucceed){
				button.setText("Retry level");
			}
			else if (button.getText().equals("Retry level") && didSucceed) {
				button.setText("Next level");
			}
			if (!button.getText().equals("Return to menu")) {
				if (!onLastLevel || (onLastLevel && !didSucceed)){
					button.render(gc, g);
				}
			} else if (onLastLevel && didSucceed && button.getText().equals("Return to menu")){
				button.render(gc, g);
			}
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame state, int g) throws SlickException {
		boolean didSucceed = ((GameStateSwitcher) state).getSuccess();
		boolean onLastLevel = ((GameStateSwitcher)state).getCurrentLevel() == GameWindowContainer.finalLevel;
		GameStateSwitcher gs = (GameStateSwitcher) state;
		// check if a button is clicked
		if (didSucceed && onLastLevel && gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){
			gs.resetMenu();
			state.enterState(GameStateSwitcher.MENU_STATE, new FadeOutTransition(Color.black, 100),new FadeInTransition());
		}
			
		for (ButtonComponent button : menuItems) {
			button.update(gc);
			if (button.isClicked()){
				int stateID = button.getStateID();
				String buttonText = button.getText();
				if (stateID != -1) {
					// load the level if it hasn't been loaded yet
					String newLevelName = "level" + (gs.getCurrentLevel() + 1); // TODO let user load own level
					if (buttonText.equals("Next level")){
						gs.loadLevel(newLevelName, true);
						gs.resetMenu();
						this.transitioning = true;
					} else if (buttonText.equals("Retry level")) {
						newLevelName = "level" + gs.getCurrentLevel();
						gs.loadLevel(newLevelName, true);
					}
					state.enterState(stateID, new FadeOutTransition(Color.black, 100),new FadeInTransition());
				} else {
					if (buttonText.equals("exit")){
						gc.exit();
					}
				}
			}
		}
		this.transitioning = false;
	}

	private void addButton(GameContainer gc, String buttonText, int stateID) {
		ButtonComponent newButton = new ButtonComponent(gc, buttonText, menuItems.size(), stateID);
		menuItems.add(newButton);
	}
	
	@Override
	public int getID() {
		return GameStateSwitcher.COMPLETION_STATE;
	}

}
